var getMyOrderList = function(that, isRefresh){
  if(!isRefresh && that.data.pageNo != 0 && that.data.pageNo >= that.data.totalPage){
    that.setData({
          loadHide: true
    });
    return;
  }
  that.setData({
      loadHide: false
  });

  var pageNum = isRefresh ? 0 : that.data.pageNo; // 页码
  var limit = that.data.pageSize;
  var offset = limit * pageNum;
  var objects = {
    tableID: getApp().globalData.TABLE_ID_ORDER,
    created_by: getApp().globalData.userInfo.userId,
    order_by: '-id,created_by',
    limit: limit,
    offset: offset
  };

  wx.BaaS.getRecordList(objects).then( (res) => {
    // success
    that.setData({
      loadHide: true,
      pageNo: that.data.pageNo+1,
      totalPage: Math.ceil(res.data.meta.total_count/that.data.pageSize),
      totalNum : res.data.meta.total_count
    });
    if(isRefresh){
      // 重新加载
      var orderList = res.data.objects;
      for (var i = 0; i < res.data.objects.length; i++) {
        res.data.objects[i].response.snapshotArr = [];
        if (res.data.objects[i].response.snapshot){
          var snapshotObj = JSON.parse(res.data.objects[i].response.snapshot);
          for (var key in snapshotObj){
            res.data.objects[i].response.snapshotArr.push(snapshotObj[key]);
          }
        } 

        // var orderItemList = res.data.objects[i].response.order_items

        // orderItemList.forEach((orderItem) => {

        //   let objects = { 
        //     tableID: getApp().globalData.TABLE_ID_ORDER_ITEM,
        //     recordID: orderItem
        //   }

        //   wx.BaaS.getRecord(objects).then(( orderItemRes ) => {

        //     var product_sku_id = orderItemRes.data.response.product_sku_id;

        //     if (!that.data.orderSku[product_sku_id]) {
        //       that.data.orderSku[product_sku_id] = true; // 正在请求
        //       wx.BaaS.getRecord({ tableID: getApp().globalData.TABLE_ID_PRODUCT_SKU, recordID: product_sku_id }).then((sku_res) => {
        //         that.data.orderSku[sku_res.data.id] = sku_res.data;
        //         that.setData({ orderSku: that.data.orderSku });
        //         var productId = sku_res.data.response.product_id;
        //         if (!that.data.orderProduct[productId]) {
        //           that.data.orderProduct[productId] = true;
        //           wx.BaaS.getRecord({ tableID: getApp().globalData.TABLE_ID_PRODUCT, recordID: productId }).then((product_res) => {
        //             that.data.orderProduct[product_res.data.id] = product_res.data;
        //             that.setData({ orderProduct: that.data.orderProduct });
        //           }), (err) => { }
        //         }
        //       }), (err) => {

        //       }
        //     }
        //   })
        // })

      }

      that.setData({
        orderList: res.data.objects
      });

    } else {
      var orderList = that.data.orderList;
      for(var i=0; i<res.data.objects.length; i++){
        res.data.objects[i].response.snapshotArr = [];
        if (res.data.objects[i].response.snapshot) {
          var snapshotObj = JSON.parse(res.data.objects[i].response.snapshot);
          for (var key in snapshotObj) {
            res.data.objects[i].response.snapshotArr.push(snapshotObj[key]);
          }
        }
        // var orderItemList = res.data.objects[i].response.order_items;

        // orderItemList.forEach((orderItem) => {

        //   let objects = {
        //     tableID: getApp().globalData.TABLE_ID_ORDER_ITEM,
        //     recordID: orderItem
        //   }

        //   console.log(objects)

        //   wx.BaaS.getRecord(objects).then((orderItemRes) => {
        //     var product_sku_id = orderItemRes.data.response.product_sku_id;
            
        //     if (!that.data.orderSku[product_sku_id]) {
        //       that.data.orderSku[product_sku_id] = true; // 正在请求
        //       wx.BaaS.getRecord({ tableID: getApp().globalData.TABLE_ID_PRODUCT_SKU, recordID: product_sku_id }).then((sku_res) => {
        //         that.data.orderSku[sku_res.data.id] = sku_res.data;
        //         that.setData({ orderSku: that.data.orderSku });
        //         var productId = sku_res.data.response.product_id;
        //         if (!that.data.orderProduct[productId]) {
        //           that.data.orderProduct[productId] = true;
        //           wx.BaaS.getRecord({ tableID: getApp().globalData.TABLE_ID_PRODUCT, recordID: productId }).then((product_res) => {
        //             that.data.orderProduct[product_res.data.id] = product_res.data;
        //             that.setData({ orderProduct: that.data.orderProduct });
        //           }), (err) => { }
        //         }
        //       }), (err) => {

        //       }
        //     }
        //   })
        // })

        orderList.push(res.data.objects[i]);
      }
      that.setData({
        orderList: orderList
      });
    }
  }, (err) => {
    // err
  });
}

var getMyInfo = function(callBack){
    var objects = {
        tableID : getApp().globalData.TABLE_ID_ADDRESS_BOOK,
        created_by : getApp().globalData.userInfo.userId
    };
    wx.BaaS.getRecordList(objects).then( (res) => {
        // success
        if(res.data.objects.length > 0) callBack(res.data.objects[0]);
    }, (err) => {
        // err
        console.log(err);
    });
}

var addMyInfo = function(dataObj, callBack){
    let tableID = getApp().globalData.TABLE_ID_ADDRESS_BOOK;
    let data = {
        "name": dataObj.name,
        "company": dataObj.company,
        "phone": dataObj.phone,
        "address" : dataObj.address,
        "status"  : 'invalid'
    };
    let objects = {
        tableID,
        data
    };
    wx.BaaS.createRecord(objects).then( (res) => {
        // success
        console.log(res);
        callBack(res.data);
    }, (err) => {
        // err
    });
}
var updateMyInfo = function(recordId, dataObj, callBack){
    let tableID = getApp().globalData.TABLE_ID_ADDRESS_BOOK;
    let recordID = recordId;
    let data = {
        "name": dataObj.name,
        "company": dataObj.company,
        "phone": dataObj.phone,
        "address" : dataObj.address,
        "status"  : 'invalid'
    }
    let objects = {
        tableID,
        recordID,
        data
    };
    wx.BaaS.updateRecord(objects).then( (res) => {
        // success
        console.log(res);
        callBack(res.data);
    }, (err) => {
        // err
    });
}

var getMyFavoriteList = function(that){
    var objects = {
        tableID : getApp().globalData.TABLE_ID_PRODUCT_FAVORITE,
        created_by : getApp().globalData.userInfo.userId
    };
    wx.BaaS.getRecordList(objects).then( (res) => {
        // success
        for(var i = 0; i < res.data.objects.length; i++){
            var tableID = getApp().globalData.TABLE_ID_PRODUCT;
            var recordID = res.data.objects[i].response.product_id;
            if(!that.data.favoriteProduct[recordID]){
                that.data.favoriteProduct[recordID] = true;
                wx.BaaS.getRecord({tableID: tableID, recordID:recordID}).then( (res) => {
                    // success
                    that.data.favoriteProduct[recordID] = res.data;
                    var favoriteList = that.data.favoriteList;
                    favoriteList.push(res.data);
                    if(res.data.response.parent_id){
                        that.setData({favoriteClothNum: that.data.favoriteClothNum + 1});
                    }else{
                        that.setData({favoriteFabricNum: that.data.favoriteFabricNum + 1});
                    }
                    that.setData({favoriteList: favoriteList});
                }, (err) => {
                // err
                });
            }
            
        }
    }, (err) => {
        // err
        console.log(err);
    });
}

var delMyFavorite = function(favoriteId, callBack){
    let tableID = getApp().globalData.TABLE_ID_PRODUCT_FAVORITE;
    let recordID = favoriteId;
    let objects = {
      tableID,
      recordID
    };
    wx.BaaS.deleteRecord(objects).then( (res) => {
      // success
      console.log(res);
      callBack(true);
    }, (err) => {
      // err
    });
}

module.exports = {
  getMyOrderList: getMyOrderList,
  getMyInfo: getMyInfo,
  addMyInfo: addMyInfo,
  updateMyInfo: updateMyInfo,
  getMyFavoriteList: getMyFavoriteList,
  delMyFavorite: delMyFavorite
}